#!/usr/bin/env python3
"""
配置验证脚本
运行: python verify_config.py
"""

import os
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

def verify_directory_structure():
    """验证目录结构完整性"""
    print("🔍 检查目录结构...")
    
    required_dirs = [
        'core',
        'gateways', 
        'strategies',
        'data_feeds',
        'risk_management',
        'common',
        'config',
        'tests'
    ]
    
    missing_dirs = []
    for dir_name in required_dirs:
        if not (project_root / dir_name).exists():
            missing_dirs.append(dir_name)
            print(f"❌ 目录缺失: {dir_name}")
        else:
            print(f"✅ 目录存在: {dir_name}")
    
    return len(missing_dirs) == 0

def verify_config_file():
    """验证配置文件"""
    print("\n📁 检查配置文件...")
    
    config_file = project_root / 'config' / 'default.yaml'
    if not config_file.exists():
        print("❌ 配置文件缺失: config/default.yaml")
        return False
    
    try:
        import yaml
        with open(config_file, 'r', encoding='utf-8') as f:
            config = yaml.safe_load(f)
        
        print("✅ 配置文件验证通过")
        return True
        
    except Exception as e:
        print(f"❌ 配置文件错误: {e}")
        return False

def verify_gateway_connection():
    """验证网关连接"""
    print("\n🔗 测试网关连接...")
    
    try:
        from gateways.backtest_gateway import BacktestGateway
        from common.data_models import OrderRequest, OrderType
        
        # 创建回测网关实例
        gateway = BacktestGateway({'initial_capital': 100000})
        gateway.connect()
        print("✅ 回测网关连接成功")
        
        # 测试订单功能
        order = OrderRequest(
            symbol="BTC/USDT",
            order_type=OrderType.LIMIT,
            volume=1.0,
            price=50000.0
        )
        
        result = gateway.send_order(order)
        print(f"✅ 订单测试成功 - 订单ID: {result.order_id}")
        
        return True
        
    except Exception as e:
        print(f"❌ 网关测试失败: {e}")
        return False

def main():
    """主验证函数"""
    print("🎯 开始系统完整性验证")
    print("=" * 50)
    
    checks = [
        verify_directory_structure,
        verify_config_file, 
        verify_gateway_connection
    ]
    
    results = []
    for check in checks:
        try:
            result = check()
            results.append(result)
        except Exception as e:
            print(f"❌ 检查过程出错: {e}")
            results.append(False)
    
    print("\n" + "=" * 50)
    print("📊 验证结果汇总:")
    
    success_count = sum(results)
    total_count = len(results)
    
    if success_count == total_count:
        print("🎉 所有验证通过！系统就绪！")
        return True
    else:
        print(f"⚠️  {success_count}/{total_count} 项验证通过")
        print("💡 请修复上述问题后重新验证")
        return False

if __name__ == "__main__":
    success = main()
    input("按Enter键退出...")